import unittest
import time
import threading
from threadpool import ThreadPool, makeRequests


def test_task(name):
    print(f"{threading.current_thread().name}:",name)
    time.sleep(10)


def get_testcase():
    suite = unittest.TestSuite()
    suite.addTests([TestTask.test01,TestTask.test02,TestTask.test03,TestTask.test04,
                   TestTask.test05,TestTask.test06,TestTask.test07,
                   TestTask.test08,TestTask.test09,TestTask.test10])
    print(suite)
    return suite


def action(suite):
    runner = unittest.runner.TextTestRunner()
    runner.run(suite)


class TestTask(unittest.TestCase):
    def test01(self):
        test_task(1)
    def test02(self):
        test_task(2)
    def test03(self):
        test_task(3)
    def test04(self):
        test_task(4)
    def test05(self):
        test_task(5)
    def test06(self):
        test_task(6)
    def test07(self):
        test_task(7)
    def test08(self):
        test_task(8)
    def test09(self):
        test_task(9)
    def test10(self):
        test_task(10)


def main():
    # 启动10个线程运行
    threadpool_task = ThreadPool(10)
    # 获取测试用例
    test_cases = get_testcase()
    # 初始化要执行的任务
    requests = makeRequests(action, test_cases, callback=None)
    # 将任务添加到线程池中，并执行
    [threadpool_task.putRequest(req) for req in requests]
    # 等待任务完成
    threadpool_task.wait()


if __name__ == '__main__':
    start_time = time.time()
    main()
    print('一共执行了%d second' % (time.time() - start_time))
